package com.zc.app.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zc.app.sys.dao.mapper.SystemUserPeerMapper;
import com.zc.app.sys.dao.peer.SystemUserPeer;
import com.zc.app.sys.dao.peer.User;
import com.zc.framework.common.bean.MyPageInfo;
import com.zc.framework.shiro.bean.Principal;
import com.zc.framework.shiro.bean.SystemAuthorizingUser;
import com.zc.framework.utils.MD5Utils;
import org.apache.ibatis.session.RowBounds;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.UnavailableSecurityManagerException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zc on 2016/11/23.
 */
@Service
public class SystemUserService {
    private static Logger logger = LogManager.getLogger(SystemUserService.class);

    @Autowired
    private SystemUserPeerMapper systemUserPeerMapper;

    public int createUser(SystemUserPeer userPeer) {
        userPeer.setLoginPassword(MD5Utils.getMD5(userPeer.getLoginPassword()));
        return systemUserPeerMapper.insert(userPeer);
    }

    public SystemUserPeer selectByLoginName(String loginName){
        return systemUserPeerMapper.selectByLoginName(loginName);
    }

    public SystemUserPeer selectByPrimaryKey(Integer accountId){
        return systemUserPeerMapper.selectByPrimaryKey(accountId);
    }

    public int deleteByPrimaryKey(Integer accountId){
        return systemUserPeerMapper.deleteByPrimaryKey(accountId);
    }

    public int updateByPrimaryKeySelective(SystemUserPeer systemUserPeer) {
        return systemUserPeerMapper.updateByPrimaryKeySelective(systemUserPeer);
    }


    /**
     * 分页显示用户信息
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    public MyPageInfo<SystemUserPeer> querySysUsers(SystemUserPeer user,int pageNum, int pageSize){
//        RowBounds rowBounds = new RowBounds(1, 10);
        PageHelper.startPage(pageNum,pageSize);
        return new MyPageInfo<SystemUserPeer>(systemUserPeerMapper.querySysUsers(user));
    }

    public List<SystemUserPeer> querySysUsers(SystemUserPeer user){
        return systemUserPeerMapper.querySysUsers(user);
    }


    public SystemUserPeer getCurrentUser(){
        SystemUserPeer user = null;
        Subject subject = SecurityUtils.getSubject();

        SecurityUtils.getSubject().getPrincipal();

        SystemAuthorizingUser principal = (SystemAuthorizingUser)subject.getPrincipal();
        if(principal != null) {
            user = selectByLoginName(principal.getLoginName());
//            CacheManager.putCache("session_usr", user);
        }

        if(user == null) {
            user = new SystemUserPeer();
            try {
                SecurityUtils.getSubject().logout();
            } catch (UnavailableSecurityManagerException var4) {
            } catch (InvalidSessionException var5) {
            }
        }

        return user;
    }

}
